![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Bug Proofing Visual Basic: A Guide to Error Handling and Prevention
Mix Data Types CautiouslyBe careful when you use variables of different data types in a single expression. Visual Basic converts them into compatible data types before it operates on them, but it may not always use the types you expect. The results are sometimes far from obvious. For example, the following four assignment statements look like they do the same thing, but their results depend on the type of operator and the data types of the operands. In the first statement, Visual Basic sees two integers combined using the & operator. The & operator is a string operator, so the program converts both of the numbers into strings. It then concatenates the strings to give a string result with the value 39. In the second statement, Visual Basic finds two integers combined with the + operator. Integers are compatible with the + operator, so it simply adds the numbers as integers. The integer result is 12. In the third statement, Visual Basic must add a string to an integer. Because the operator is +, the system decides that this is a numeric operation so it converts the string 3 into the double 3.0. Now it must add an integer to a double. Since these have different data types, Visual Basic must promote one value so they match. It can change an integer into a double without losing any precision, so that is what it does. It converts the integer 9 into the double 9.0 and adds this to 3.0. The result is a double with value 12.0. Finally, in the fourth statement, Visual Basic sees two strings. Because they have the same data type, Visual Basic can combine them without any promotion. It adds the strings by concatenating them. The result is the string 39.
Dim a As Variant
Dim b As Variant
Dim c As Variant
Dim d As Variant
a = 3 & 9 Result: string 39
b = 3 + 9 Result: integer 12
c = 3 + 9 Result: double 12.0
d = 3 + 9 Result: string 39
These four similar statements produce very different results. To be certain of the type of result you get when you mix data types, use the conversion functions CInt, CLng, CStr, and so forth to force Visual Basic to do exactly what you want it to do. Convert all operands into the data type you want the eventual result to have. Also be sure you use the right kind of operators for that data type. Use + for numeric values and & to concatenate strings. Following these practices makes the code more obvious and will help those who later read the code understand what is happening.
Dim b As Variant
Dim c As Variant
Dim d As Variant
a = CStr(3) & CStr(9) Result: string 39
b = CDbl(3) + CDbl(9) Result: double 12.0
c = CInt(3) + 9 Result: integer 12
d = CDbl(3) + CDbl(9) Result: double 12.0
Use & and + CorrectlyAs the examples in the previous section show, the result of an expression depends in part on the operators the expression uses. If you use the wrong operator, you may get an unexpected result. To concatenate two strings and produce a new string, use the & operator. To add two numeric values and produce a new numeric value, use the + operator. The following two lines of code produce completely different results even though they differ only in their operator. a = 8 & 5 Result: string 85 b = 8 + 5 Result: integer 13 Use the right operators to keep the code obvious. Mix Booleans and Integers CautiouslyBe extra cautious when you mix Boolean and integer values. The Boolean operators And, Or, Not, and so forth look just like the corresponding bitwise operators you can use to combine and compare numeric values. This can make statements that mix Boolean and integer values very confusing. For example, if A and B are integers, then the following statement sets the bits in C to 1 where both A and B have bit values 1. Bits where either A or B has value 0 are set to 0. C = A And B On the other hand, if D and E are Boolean values, the following statement sets the variable F to True if D and E are both True. F = D And E Matters are further complicated by the fact that Visual Basic treats an integer as logically False if it has the value 0, and logically True if it has any other value. Now consider the following statement: G = H And I Or J And K The value of G depends on the data types of all of the variables. Even if you know their data types, evaluating this expression would be confusing. Make this expression understandable by using the CInt and CBool conversion functions. Use parentheses to make the evaluation order obvious. The following code sets G to True if H and I have any common bit set, or if J and K have any common bit set.
Dim G As Boolean
Dim H As Integer
Dim I As Integer
Dim J As Integer
Dim K As Integer
:
Initialize H, I, J, and K.
:
G = CBool(H And I) Or CBool(J And K)
Using the CBool conversion functions make it obvious that the result is a Boolean value. The CBool function also groups the operands so the evaluation order of the And and Or operators is obvious. Parenthesize Complex ExpressionsUse parentheses to make the evaluation order of complex expressions obvious. This is particularly important when you mix numeric operations with other types of operations. Intermingled integer and Boolean operations are probably the most confusing. Most people know that multiplication and division take precedence over addition and subtraction. Most programmers can figure out that 10 + 2 * 3 is the same as 10 + (2 * 3) = 16 and not (10 + 2) * 3 = 36. Even so, adding parentheses makes the statement even more obvious. Mixing Boolean and bitwise operations makes things much more confusing. Suppose A, B, C, and D are integers. The following lines of code are intended to determine whether A and B share a common bit and C and D share a common bit. Instead, the first line sets the bits in variable E that correspond to bits that are set in all four variables. The second line gives the correct code. E = A And B And C And D E = CBool(A And B) And CBool(C And D) Use parentheses and conversion functions to make expressions obvious, particularly when you combine values or operations of different data types. Use Left$Visual Basic provides string and variant versions of several functions including LCase, Left, LeftB, Mid, Right, Chr, Str, and UCase. The string versions end with a $, as in Left$ and Str$, and return strings. The variant versions including Left and Str return variants containing strings. If you want to produce a variant, use the variant version of the function. If you want a string, use the string version ending in $. Using the correct functions can prevent confusion later.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|